package cn.iocoder.aics.module.member.controller.admin.chathistory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.aics.framework.common.pojo.PageParam;
import cn.iocoder.aics.framework.common.pojo.PageResult;
import cn.iocoder.aics.framework.common.pojo.CommonResult;
import cn.iocoder.aics.framework.common.util.object.BeanUtils;
import static cn.iocoder.aics.framework.common.pojo.CommonResult.success;

import cn.iocoder.aics.framework.excel.core.util.ExcelUtils;

import cn.iocoder.aics.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.aics.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.aics.module.member.controller.admin.chathistory.vo.*;
import cn.iocoder.aics.module.member.dal.dataobject.chathistory.ChatHistoryDO;
import cn.iocoder.aics.module.member.service.chathistory.ChatHistoryService;

@Tag(name = "管理后台 - 聊天记录")
@RestController
@RequestMapping("/member/chat-history")
@Validated
public class ChatHistoryController {

    @Resource
    private ChatHistoryService chatHistoryService;

    @PostMapping("/create")
    @Operation(summary = "创建聊天记录")
    @PreAuthorize("@ss.hasPermission('member:chat-history:create')")
    public CommonResult<Long> createChatHistory(@Valid @RequestBody ChatHistorySaveReqVO createReqVO) {
        return success(chatHistoryService.createChatHistory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新聊天记录")
    @PreAuthorize("@ss.hasPermission('member:chat-history:update')")
    public CommonResult<Boolean> updateChatHistory(@Valid @RequestBody ChatHistorySaveReqVO updateReqVO) {
        chatHistoryService.updateChatHistory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除聊天记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:chat-history:delete')")
    public CommonResult<Boolean> deleteChatHistory(@RequestParam("id") Long id) {
        chatHistoryService.deleteChatHistory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得聊天记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:chat-history:query')")
    public CommonResult<ChatHistoryRespVO> getChatHistory(@RequestParam("id") Long id) {
        ChatHistoryDO chatHistory = chatHistoryService.getChatHistory(id);
        return success(BeanUtils.toBean(chatHistory, ChatHistoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得聊天记录分页")
    @PreAuthorize("@ss.hasPermission('member:chat-history:query')")
    public CommonResult<PageResult<ChatHistoryRespVO>> getChatHistoryPage(@Valid ChatHistoryPageReqVO pageReqVO) {
        PageResult<ChatHistoryDO> pageResult = chatHistoryService.getChatHistoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatHistoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出聊天记录 Excel")
    @PreAuthorize("@ss.hasPermission('member:chat-history:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatHistoryExcel(@Valid ChatHistoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatHistoryDO> list = chatHistoryService.getChatHistoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "聊天记录.xls", "数据", ChatHistoryRespVO.class,
                        BeanUtils.toBean(list, ChatHistoryRespVO.class));
    }

}